using System;
using System.Globalization;
using System.Windows.Forms;
using Accounting.Model;

namespace EIM_Accounting
{
    /// <summary>
    /// 合同 合同管理 合同编辑
    /// </summary>
    public partial class CP010102 : Form
    {
        EIM_Accounting.BLL.CP010102 cp010102 = new BLL.CP010102();
        public string contactId = string.Empty;

        public CP010102()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 限制输入为浮点数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void input_float_Press(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
            {
                e.Handled = true;
            }

            if (e.KeyChar == 46)                       //小数点
            {
                string txtValue = ((System.Windows.Forms.TextBox)sender).Text;

                if (txtValue.Length <= 0)
                    e.Handled = true;           //小数点不能在第一位
                else
                {
                    float f;
                    float oldf;
                    bool b1 = false, b2 = false;
                    b1 = float.TryParse(txtValue, out oldf);
                    b2 = float.TryParse(txtValue + e.KeyChar.ToString(), out f);
                    if (b2 == true)
                    {
                        if (b1 == false)
                            e.Handled = true;
                        else
                            e.Handled = false;
                    }
                }
            }
        }

        private void btnSave_Click(object sender, System.EventArgs e)
        {
            Contract contractModel = new Contract();
            if (contactId != string.Empty)
            {
                contractModel = cp010102.GetModelById(contactId);
            }
            //合约公司
            contractModel.SignComp = txtSignComp.Text;
            // 预订开始日
            contractModel.PlanStartDate = dtpPlanStartDate.Value.ToString("yyyyMMdd");
            // 预订结束日
            contractModel.PlanEndDate = dtpPlanEnd.Value.ToString("yyyyMMdd");
            //普通加班、周末加班、节假日加班的每小时费用
            if (txtNormalFee.TextLength > 0)
            {
                contractModel.NormalAddFee = decimal.Parse(txtNormalFee.Text);
            }

            if (txtHolidayFee.TextLength > 0)
            {
                contractModel.HolidayAddFee = decimal.Parse(txtHolidayFee.Text);
            }
            else
            {
                contractModel.HolidayAddFee = -1;
            }
            if (txtWeekendFee.TextLength > 0)
            {
                contractModel.WeekEndAddFee = decimal.Parse(txtWeekendFee.Text);
            }
            else
            {
                contractModel.WeekEndAddFee = -1;
            }
            //单金（月）
            if (txtSingle.TextLength > 0)
            {
                contractModel.SingleMoney = decimal.Parse(txtSingle.Text);
            }

            //总金额
            if (txtTotal.TextLength > 0)
            {
                contractModel.TotalMoney = decimal.Parse(txtTotal.Text);
            }
            //备注
            contractModel.Remark = txtRemark.Text;

            if (contactId != string.Empty)
            {
                cp010102.UpdateContractBaseInfo(contractModel);
            }
            else
            {
                contractModel.FinishFlag = "0";
                cp010102.AddContract(contractModel);
            }

            this.Close();
        }

        private void btnClose_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        private void CP010102_Load(object sender, System.EventArgs e)
        {
            if (contactId != string.Empty)
            {
                Contract contract = cp010102.GetModelById(contactId);

                if (contract == null) { contactId = string.Empty; return; }
                btnDel.Enabled = true;
                txtSignComp.Text = contract.SignComp;
                dtpPlanStartDate.Value = DateTime.ParseExact(contract.PlanStartDate, "yyyyMMdd", CultureInfo.InvariantCulture);
                dtpPlanEnd.Value = DateTime.ParseExact(contract.PlanEndDate, "yyyyMMdd", CultureInfo.InvariantCulture);
                if (contract.NormalAddFee != null)
                {
                    txtNormalFee.Text = contract.NormalAddFee.Value.ToString("#,###.##");
                } if (contract.WeekEndAddFee != null)
                {
                    txtWeekendFee.Text = contract.WeekEndAddFee.Value.ToString("#,###.##");
                } if (contract.HolidayAddFee != null)
                {
                    txtHolidayFee.Text = contract.HolidayAddFee.Value.ToString("#,###.##");
                }
                if (contract.SingleMoney != null)
                {
                    txtSingle.Text = contract.SingleMoney.Value.ToString("#,###.##");
                }
                if (contract.TotalMoney != null)
                {
                    txtTotal.Text = contract.TotalMoney.Value.ToString("#,###.##");
                }

                txtRemark.Text = contract.Remark;
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            cp010102.DelModel(contactId);
        }
    }
}